// License Notification
//======================================================
// Copyright (c) 2010 Channel Intelligence Inc.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// ======================================================




using System.Xml.Linq;

namespace CIP4.Common
{
    public static class XElementExtension
    {
        public static XElement FirstElement(this XElement element)
        {
            if (element == null)
                return null;
            foreach (XElement child in element.Elements())
                return child;
            return null;
        }
        public static XElement LastElement(this XElement element)
        {
            if (element == null)
                return null;
            XNode last = element.LastNode;
            while (last != null)
            {
                if (last is XElement)
                    return (XElement)last;
                last = last.PreviousNode;
            }
            return null;
        }
        public static XElement NextSiblingElement(this XElement element)
        {
            if (element == null)
                return null;
            XNode node = element.NextNode;
            while (node != null)
            {
                if (node is XElement)
                    return (XElement)node;
                node = node.NextNode;
            }
            return null;
        }
        public static XElement PreviousSiblingElement(this XElement element)
        {
            if (element == null)
                return null;
            XNode node = element.PreviousNode;
            while (node != null)
            {
                if (node is XElement)
                    return (XElement)node;
                node = node.PreviousNode;
            }
            return null;
        }
    }
}